#include "RenderTarget.h"
#include "..\base\Engine.h"
#include "..\content\Texture.h"
#include "..\lib\container\String.h"

namespace ds {

RenderTarget::RenderTarget() {
}


RenderTarget::~RenderTarget() {
}

ResourceHandle RenderTarget::create(GraphicsDevice* device,int index) {
	int width = gEngine->getWidth();
	int height = gEngine->getHeight();
	device->get()->CreateTexture(width,
                                 height,
                                 1,
                                 D3DUSAGE_RENDERTARGET,
                                 D3DFMT_X8R8G8B8,
                                 D3DPOOL_DEFAULT,
                                 &m_RenderTexture,
                                 NULL);
	String name = "RT_Texture";
	name.append(index);
	Texture* t = new Texture(name.c_str(),m_RenderTexture,width,height);
	ResourceHandle rh = gEngine->getResourceManager().createTexture(name.c_str(),t);
	m_RenderTexture->GetSurfaceLevel(0,&m_RenderSurface);
	return rh;
}

void RenderTarget::restoreBackBuffer() {
	gEngine->getRenderer()->getDevice()->SetRenderTarget(0,m_BackBuffer);
}

void RenderTarget::saveBackBuffer() {
	gEngine->getRenderer()->getDevice()->GetRenderTarget(0,&m_BackBuffer);
}

void RenderTarget::setTargets() {
	gEngine->getRenderer()->getDevice()->SetRenderTarget(0,m_RenderSurface);
}

};
